If I have a function declared with two default parameters, like this:
const doSomething = (defaultVal = 'me default', useThis = false) => {
/* ... */
};
And I want to call the function passing in an argument for the second parameter, but have the first argument just use the default value:
doSomething(???, true);
Is there some syntax for telling the function to use the default parameter, or does a real value need to be passed in?
Pass in undefined
const doSomething = (defaultVal = 'me default', useThis = false) => {
console.log({ defaultVal, useThis });
};
doSomething(undefined, true)
There is no syntax or key for that purpose. You should use default parameters as last. For that example if useThis parameter will be given a value for everytime you can give useThis as first argument.
Alternatively you can give an object as parameter. Inside the function you can merge your template (default valued object) and argument object then use it in you function.
const fn = (props) => {
const newProps = {
defaultVal : "default",
useThis : false,
...props
}
}